package com.atguigu02.reference;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class MethodResTest {
    //情况一:  对象::实例方法
//Consumer中的void accept(T t)
// PrintStream中的void println(T t)
    @Test
    public void t1(){
        Consumer<String> con1=new Consumer<String>() {
            @Override
            public void accept(String s) {//这两行的函数的形参列表和返回值类型一致
                System.out.println(s);

            }
        };
        con1.accept("nihao");

        Consumer<String> con2=s-> System.out.println(s);
        con2.accept("nihao1");

        Consumer<String> con3= System.out::println;
        con3.accept("hello");


    }

    @Test
    public void t2(){
        Employee emp = new Employee(1001,"马化腾", 34, 6000.38);
        //1.
        Supplier<String> sup1 = new Supplier<String>() {//这两行的函数的形参列表和返回值类型一致
            public String get() {
                return emp.getName();
            }
        };
        System.out.println(sup1.get());
//2. lambda表达式
        Supplier<String> sup2 = () -> emp.getName();
        System.out.println(sup2.get());
//3．方法引用
        Supplier<String> sup3 = emp ::getName;

    }

    @Test
    public void t3(){
        Comparator<Integer> com1=new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {//这两行的函数的形参列表和返回值类型一致
                return Integer.compare(o1,o2);
            }
        };
        System.out.println(com1.compare(23, 12));

        Comparator<Integer> com2=((o1, o2) -> Integer.compare(o1,o2));
        System.out.println(com2.compare(23, 12));

        Comparator<Integer> com3=Integer::compare;
        System.out.println(com3.compare(12, 23));

    }

    @Test
    public void t4(){
        Comparator<String> com1=new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare("asd","swq"));

        Comparator<String> com2=((o1, o2) -> o1.compareTo(o2));
        //System.out.println(com2);
        System.out.println(com2.compare("asd", "swq"));

        Comparator<String> com3=String::compareTo;
        System.out.println(com3.compare("asd", "swq"));

    }

}
